import java.nio.ByteBuffer;
import java.util.Arrays;


public class ByteUtils {
	
//	private static int bytes2Int(byte[] bytes, int offset,int length) {
//		ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, offset, length);
//		return byteBuffer.getInt();
//	}
//	
//	private static byte[] int2Bytes(int value,int length){
//		ByteBuffer byteBuffer = ByteBuffer.allocate(length);
//		byteBuffer.putInt(value);
//		return byteBuffer.array();
//	}
	
	public static String byte2String(byte[] bytes,int offset,int length){
		return new String(bytes,offset,length);
	}
	
	public static byte[] stringtoBytes(String value){
		return value.getBytes();
	}
	
	public static byte[] replaceBytes(byte[] srcBytes,int offset,int length,byte[] replaceBytes){
		byte[] destBytes = new byte[srcBytes.length + replaceBytes.length - length];
		System.arraycopy(srcBytes, offset, destBytes, 0, offset);
		System.arraycopy(replaceBytes,0,destBytes,offset,replaceBytes.length);
		System.arraycopy(srcBytes, offset + length, destBytes, offset + replaceBytes.length, srcBytes.length - offset - length);
		return destBytes;
	}

	
	public static byte[] int2Bytes(int value,int length){
		byte[] bytes = new byte[length];
		for (int i = 0; i < bytes.length; i++) {
			bytes[length -i -1] = (byte)((value >> 8 * i) & 0xff);
		}
		return bytes;
	}
	
	public static int bytes2Int(byte[] bytes,int offset,int length){
		int sum = 0;
		int end = offset + length;
		for (int i = offset; i < end; i++) {
			int n = (int)bytes[i] & 0xff;
			n <<= (--length) * 8;
			sum = n + sum;
		}
		return sum;
	}
	
	public static void main(String[] args) {
//		System.out.println(Arrays.toString(ByteUtils.int2Bytes(288,4)));
		System.out.println(Arrays.toString(ByteUtils.int2Bytes(288,2)));
//		System.out.println(ByteUtils.bytes2Int(ByteUtils.int2Bytes(288, 4),2,2));
		System.out.println(ByteUtils.bytes2Int(ByteUtils.int2Bytes(288, 2),0,2));
	}
	
	
}
